package com.suda.sudaexamsystem.controller;

import com.suda.sudaexamsystem.dto.QuestionDto;
import com.suda.sudaexamsystem.dto.RecordDto;
import com.suda.sudaexamsystem.dto.RecordItemDto;
import com.suda.sudaexamsystem.pojo.Question;
import com.suda.sudaexamsystem.pojo.Record;
import com.suda.sudaexamsystem.pojo.RecordItem;
import com.suda.sudaexamsystem.service.QuestionService;
import com.suda.sudaexamsystem.service.RecordService;
import com.suda.sudaexamsystem.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/record")
@Slf4j
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private QuestionService questionService;

    // 批改练习并添加答题记录
    @PostMapping(value = "/addRecord", produces = {"application/json;charset=UTF-8"})
    public AjaxResult addRecord(@RequestBody Record record) {
        // 总分
        BigDecimal score = new BigDecimal(0);
        for (RecordItem item : record.getRecordItems()) {
            item.setRecord(record);
            // 批改题目
            Question question = questionService.findById(item.getQuestion().getId());
            boolean isCorrect = question.getAnswer().trim().equalsIgnoreCase(item.getAnswer().trim());
            if (isCorrect) {
                score = score.add(question.getScore());
            }
            item.setCorrect(isCorrect);
        }
        record.setScore(score);
        log.info("score：" + score + "分");
        recordService.addRecord(record);
        return AjaxResult.success();
    }

    // 查询指定练习记录
    /*
    {"msg":"操作成功","code":200,"data":{"id":1,"paper_name":"软件项目管理","paper_id":1,"record_item_dto_list":[{"id":1,"question":{"id":1,"order_id":1,"name":"如果项目失败，负主要责任的是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"决策层","option_b":"项目发起人","option_c":"项目经理","option_d":"管理层","answer":"C"},"answer":"C","correct":true},{"id":2,"question":{"id":2,"order_id":2,"name":"在项目管理中，管理层扮演的角色是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"财务监控者",
    "option_b":"独裁者","option_c":"插手实施工作者","option_d":"支持者","answer":"D"},"answer":"D",
    "correct":true},{"id":3,"question":{"id":3,"order_id":3,"name":"下列哪个选项时我们做报告时最应该担心的（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"听众","option_b":"报告内容与信息含量","option_c":"提议实施的技术",
    "option_d":"报告时间长短","answer":"B"},"answer":"C","correct":false},{"id":4,"question":{"id":4,"order_id":4,"name":"如果你是IT项目经理，和客户经理们谈话时应该重点讲述的是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"项目结果","option_b":"项目经理在公司内的业绩",
    "option_c":"技术速度","option_d":"工作效率","answer":"A"},"answer":"A","correct":true},{"id":5,"question":{"id":5,"order_id":5,"name":"项目某个阶段结束的标志是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"WBS内确定的日期","option_b":"项目团队的派对",
    "option_c":"达到指定的里程碑","option_d":"最终的交付成果","answer":"C"},"answer":"C","correct":true}],"score":4.00,"start_time":1703048400000,"end_time":1703057400000}}
     */
    @GetMapping(value = "/get/{recordId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult getRecord(@PathVariable("recordId") int recordId) {
        Record record = recordService.findById(recordId);
        RecordDto recordDto = new RecordDto();
        recordDto.setId(record.getId());
        recordDto.setPaperName(record.getPaper().getName());
        recordDto.setPaperId(record.getPaper().getId());
        recordDto.setScore(record.getScore());
        recordDto.setStartTime(record.getStartTime());
        recordDto.setEndTime(record.getEndTime());
        List<RecordItemDto> recordItemDtoList = recordDto.getRecordItemDtoList();
        for (RecordItem item : record.getRecordItems()) {
            RecordItemDto recordItemDto = new RecordItemDto();
            recordItemDto.setId(item.getId());
            // 设置question dto
            Question question = item.getQuestion();
            QuestionDto questionDto = new QuestionDto();
            questionDto.setId(question.getId());
            questionDto.setName(question.getName());
            questionDto.setScore(question.getScore());
            questionDto.setType(question.getType());
            questionDto.setAnswer(question.getAnswer());
            questionDto.setOptionA(question.getOptionA());
            questionDto.setOptionB(question.getOptionB());
            questionDto.setOptionC(question.getOptionC());
            questionDto.setOptionD(question.getOptionD());
            questionDto.setOrderId(question.getOrderId());
            recordItemDto.setQuestion(questionDto);
            recordItemDto.setCorrect(item.isCorrect());
            recordItemDto.setAnswer(item.getAnswer());
            recordItemDtoList.add(recordItemDto);
        }
        return AjaxResult.success(recordDto);
    }
    // 查询所有练习记录
    // 返回的json和上面查询单个类似 就是把单个对象变成了集合
    @GetMapping(value = "/getAll/{userId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult getRecords(@PathVariable("userId") int userId) {
        List<Record> recordList = recordService.findRecordsByUserId(userId);
        List<RecordDto> recordDtoList = new ArrayList<>();
        for (Record record : recordList) {
            RecordDto recordDto = new RecordDto();
            recordDto.setId(record.getId());
            recordDto.setPaperName(record.getPaper().getName());
            recordDto.setPaperId(record.getPaper().getId());
            recordDto.setScore(record.getScore());
            recordDto.setStartTime(record.getStartTime());
            recordDto.setEndTime(record.getEndTime());
            List<RecordItemDto> recordItemDtoList = recordDto.getRecordItemDtoList();
            for (RecordItem item : record.getRecordItems()) {
                RecordItemDto recordItemDto = new RecordItemDto();
                recordItemDto.setId(item.getId());
                // 设置question dto
                Question question = item.getQuestion();
                QuestionDto questionDto = new QuestionDto();
                questionDto.setId(question.getId());
                questionDto.setName(question.getName());
                questionDto.setScore(question.getScore());
                questionDto.setType(question.getType());
                questionDto.setAnswer(question.getAnswer());
                questionDto.setOptionA(question.getOptionA());
                questionDto.setOptionB(question.getOptionB());
                questionDto.setOptionC(question.getOptionC());
                questionDto.setOptionD(question.getOptionD());
                questionDto.setOrderId(question.getOrderId());
                recordItemDto.setQuestion(questionDto);
                recordItemDto.setCorrect(item.isCorrect());
                recordItemDto.setAnswer(item.getAnswer());
                recordItemDtoList.add(recordItemDto);
            }
            recordDtoList.add(recordDto);
        }
        return AjaxResult.success(recordDtoList);
    }
}
